// 文件路径: com.city.java.feedbacks.service.impl.FeedbackProcessingServiceImpl.java
package com.city.java.feedbacks.service.impl;

import com.city.java.base.response.JsonResult;
import com.city.java.base.response.StatusCode;
import com.city.java.feedbacks.mapper.FeedbackProcessingMapper;
import com.city.java.feedbacks.mapper.FeedbackMapper;
import com.city.java.feedbacks.pojo.entity.FeedbackProcessing;
import com.city.java.feedbacks.pojo.entity.Feedbacks;
import com.city.java.feedbacks.service.FeedbackProcessingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 意见反馈处理服务实现类
 */
@Slf4j
@Service
public class FeedbackProcessingServiceImpl implements FeedbackProcessingService {

    @Autowired
    private FeedbackProcessingMapper feedbackProcessingMapper;

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    public JsonResult addFeedbackProcessing(FeedbackProcessing feedbackProcessing) {
        log.debug("准备添加反馈处理记录: {}", feedbackProcessing);

        // 参数校验
        if (feedbackProcessing == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "反馈处理信息不能为空");
        }

        if (feedbackProcessing.getFeedbackId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "反馈ID不能为空");
        }

        // 设置创建时间
        if (feedbackProcessing.getCreatedAt() == null) {
            feedbackProcessing.setCreatedAt(LocalDateTime.now());
        }

        try {
            feedbackProcessingMapper.insertFeedbackProcessing(feedbackProcessing);
            log.info("反馈处理记录添加成功: 处理ID={}", feedbackProcessing.getProcessingId());
            return JsonResult.ok();
        } catch (Exception e) {
            log.error("添加反馈处理记录失败: 反馈ID={}", feedbackProcessing.getFeedbackId(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "添加反馈处理记录失败");
        }
    }

    @Override
    public JsonResult getFeedbackProcessingById(Long processingId) {
        log.debug("根据ID查询反馈处理记录: {}", processingId);

        // 参数校验
        if (processingId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "处理ID不能为空");
        }

        try {
            FeedbackProcessing feedbackProcessing = feedbackProcessingMapper.selectById(processingId);
            if (feedbackProcessing != null) {
                log.info("查询反馈处理记录成功: ID={}", processingId);
                return JsonResult.ok(feedbackProcessing);
            } else {
                log.warn("反馈处理记录不存在: ID={}", processingId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "反馈处理记录不存在");
            }
        } catch (Exception e) {
            log.error("查询反馈处理记录失败: ID={}", processingId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询反馈处理记录失败");
        }
    }

    @Override
    public JsonResult getFeedbackProcessingByFeedbackId(Long feedbackId) {
        log.debug("根据反馈ID查询反馈处理记录: {}", feedbackId);

        // 参数校验
        if (feedbackId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "反馈ID不能为空");
        }

        try {
            FeedbackProcessing feedbackProcessing = feedbackProcessingMapper.selectByFeedbackId(feedbackId);
            if (feedbackProcessing != null) {
                log.info("查询反馈处理记录成功: 反馈ID={}", feedbackId);
                return JsonResult.ok(feedbackProcessing);
            } else {
                log.warn("反馈处理记录不存在: 反馈ID={}", feedbackId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "反馈处理记录不存在");
            }
        } catch (Exception e) {
            log.error("查询反馈处理记录失败: 反馈ID={}", feedbackId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询反馈处理记录失败");
        }
    }

    @Override
    public JsonResult getFeedbackProcessingsByStatus(String status) {
        log.debug("根据状态查询反馈处理列表: {}", status);

        // 参数校验
        if (status == null || status.trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "状态不能为空");
        }

        try {
            List<FeedbackProcessing> feedbackProcessings = feedbackProcessingMapper.selectByStatus(status);
            log.info("查询状态反馈处理列表成功: 状态={}, 记录数量={}", status, feedbackProcessings.size());
            return JsonResult.ok(feedbackProcessings);
        } catch (Exception e) {
            log.error("查询状态反馈处理列表失败: 状态={}", status, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询反馈处理列表失败");
        }
    }

    @Override
    public JsonResult getAllFeedbackProcessings() {
        log.debug("查询所有反馈处理列表");

        try {
            List<FeedbackProcessing> feedbackProcessings = feedbackProcessingMapper.selectAll();
            log.info("查询所有反馈处理列表成功: 记录数量={}", feedbackProcessings.size());
            return JsonResult.ok(feedbackProcessings);
        } catch (Exception e) {
            log.error("查询所有反馈处理列表失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询反馈处理列表失败");
        }
    }

    @Override
    public JsonResult deleteFeedbackProcessingById(Long processingId) {
        log.debug("根据ID删除反馈处理记录: {}", processingId);

        // 参数校验
        if (processingId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "处理ID不能为空");
        }

        try {
            int result = feedbackProcessingMapper.deleteById(processingId);
            if (result > 0) {
                log.info("删除反馈处理记录成功: ID={}", processingId);
                return JsonResult.ok();
            } else {
                log.warn("删除反馈处理记录失败，记录不存在: ID={}", processingId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "反馈处理记录不存在");
            }
        } catch (Exception e) {
            log.error("删除反馈处理记录失败: ID={}", processingId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "删除反馈处理记录失败");
        }
    }

    @Override
    public JsonResult updateFeedbackProcessing(FeedbackProcessing feedbackProcessing, String feedbackStatus) {
        log.debug("更新反馈处理信息: {}", feedbackProcessing);

        // 参数校验
        if (feedbackProcessing == null || feedbackProcessing.getProcessingId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "处理ID不能为空");
        }

        try {
            // 检查反馈处理记录是否存在
            FeedbackProcessing existingProcessing = feedbackProcessingMapper.selectById(feedbackProcessing.getProcessingId());
            if (existingProcessing == null) {
                log.warn("更新反馈处理记录失败，记录不存在: ID={}", feedbackProcessing.getProcessingId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "反馈处理记录不存在");
            }

            // 如果需要更新处理时间（当状态变为已处理时）
            if ("processed".equals(feedbackStatus) && feedbackProcessing.getProcessedAt() == null) {
                feedbackProcessing.setProcessedAt(LocalDateTime.now());
            }

            int result = feedbackProcessingMapper.updateById(feedbackProcessing);

            // 同步更新feedbacks表中的状态（无论feedbackStatus是否为空都尝试更新）
            if (feedbackStatus != null) {
                Feedbacks feedback = new Feedbacks();
                feedback.setFeedbackId(existingProcessing.getFeedbackId());
                feedback.setStatus(Feedbacks.FeedbackStatus.fromValue(feedbackStatus));
                feedbackMapper.updateById(feedback);
                log.info("同步更新反馈状态成功: feedbackId={}, status={}", existingProcessing.getFeedbackId(), feedbackStatus);
            }

            if (result > 0) {
                log.info("更新反馈处理记录成功: ID={}", feedbackProcessing.getProcessingId());
                return JsonResult.ok();
            } else {
                log.warn("更新反馈处理记录失败: ID={}", feedbackProcessing.getProcessingId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "更新反馈处理记录失败");
            }
        } catch (Exception e) {
            log.error("更新反馈处理记录失败: ID={}", feedbackProcessing.getProcessingId(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "更新反馈处理记录失败");
        }
    }
}
